package vg.services.graph_view_manager;

import vg.interfaces.ll_services.data_base_service.data.graph.Graph;
import vg.services.graph_view_manager.interfaces.IGraphViewManager;
import vg.services.graph_view_manager.interfaces.graph_comparison.AGraphComparisonView;
import vg.services.graph_view_manager.interfaces.graph_comparison.IGraphComparisonViewFactory;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.graph_view.IGraphViewFactory;
import vg.services.graph_view_manager.realization.graph_comparison.SmartGraphComparisonViewFactory;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.SmartGraphViewFactory;

public class GraphViewManager implements IGraphViewManager {
	// Mutex
	private final Object generalMutex = new Object();
	
	// Main components
	private IGraphViewFactory graphViewFactory;
	private IGraphComparisonViewFactory graphComparisonViewFactory;
	
	public GraphViewManager() {
        // default settings
        graphViewFactory = new SmartGraphViewFactory();
        graphComparisonViewFactory = new SmartGraphComparisonViewFactory();
	}
	
	@Override
	public boolean setGraphComparisonViewFactory(IGraphComparisonViewFactory factory) {
		synchronized (generalMutex) {
			if (factory != null) {
				graphComparisonViewFactory = factory;
				return true;
			}
		}
		return false;
	}
	
	@Override
	public boolean setGraphViewFactory(IGraphViewFactory factory) {
		synchronized (generalMutex) {
			if (factory != null) {
				graphViewFactory = factory;
				return true;
			}
		}
		return false;
	}
	
	@Override
	public AGraphComparisonView newGraphComparison(Graph g1, Graph g2) {
		synchronized (generalMutex) {
		    return graphComparisonViewFactory.createGraphComparisonView(g1, g2);
        }
	}
	
	@Override
	public AGraphView newGraphView(Graph g) {
        synchronized (generalMutex) {
            return graphViewFactory.createGraphView(g);
        }
    }

    @Override
    public AGraphView newGraphView() {
        synchronized (generalMutex) {
            return graphViewFactory.createGraphView();
        }
    }

    @Override
	public AGraphView newGraphView(AGraphView graphView, Graph graph) {
        synchronized (generalMutex) {
            return graphViewFactory.createGraphView(graphView, graph);
        }
    }
}
